<script setup lang="ts">
import { ref, onMounted } from "vue";
import ReCol from "@/components/ReCol";
import { formRules } from "../utils/rule";
/** 表单字段 */
import { FormProps } from "../utils/types";
import { usePublicHooks } from "../../hooks";
import { getUserPoint } from "@/api/user";

/** 表单字段初始化 */
const props = withDefaults(defineProps<FormProps>(), {
  formInline: () => ({
    title: "新增",
    username: "",
    password: "",
    unitname: "",
    rpgname: "",
    phone: "",
    useremail: "",
    qq: "",
    point: 0,
    enable: true
  })
});
const ruleFormRef = ref();
const { switchStyle } = usePublicHooks();
const newFormInline = ref(props.formInline);

function getRef() {
  return ruleFormRef.value;
}

defineExpose({ getRef });
// onMounted(() => {
//   if (newFormInline.value.title == "查看") {
//     newFormInline.value.point = 0;
//     console.log("查看表单");
//     let userid = newFormInline.value.userid;
//     getUserPoint(userid).then(res => {
//       newFormInline.value.point = res.data.point;
//       console.log(res);
//     });
//   }
// });
</script>

<!-- ✨该表单实现了 修改和新增 两种状态表单公用 -->
<!-- 默认状态为 修改表单 -->
<!-- 仅在 newFormInline.title 的值为 新增时，显示新增表单 -->
<template>
  <el-form
    ref="ruleFormRef"
    :model="newFormInline"
    :rules="formRules"
    label-width="82px"
  >
    <el-row :gutter="30">
      <re-col
        v-if="newFormInline.title === '查看'"
        :value="12"
        :xs="24"
        :sm="24"
      >
        <el-form-item label="用户id" prop="userid">
          <el-input
            v-model="newFormInline.userid"
            clearable
            placeholder="请输入用户名称"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="newFormInline.username"
            clearable
            placeholder="请输入用户名称"
          />
        </el-form-item>
      </re-col>
      <re-col
        v-if="newFormInline.title === '新增'"
        :value="12"
        :xs="24"
        :sm="24"
      >
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="newFormInline.password"
            clearable
            placeholder="请输入用户密码"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="手机号码" prop="phone">
          <el-input
            v-model="newFormInline.phone"
            clearable
            placeholder="请输入手机号码"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="邮箱" prop="useremail">
          <el-input
            v-model="newFormInline.useremail"
            clearable
            placeholder="请输入邮箱"
          />
        </el-form-item>
      </re-col>

      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="机构名" prop="unitname">
          <el-input
            v-model="newFormInline.unitname"
            clearable
            placeholder="请输入机构名称"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="课题组" prop="rpgname">
          <el-input
            v-model="newFormInline.rpgname"
            clearable
            placeholder="请输入课题组名称"
          />
        </el-form-item>
      </re-col>

      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="QQ号码" prop="qq">
          <el-input
            v-model="newFormInline.qq"
            clearable
            placeholder="请输入QQ号码"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="用户状态">
          <el-switch
            v-model="newFormInline.enable"
            inline-prompt
            :active-value="true"
            :inactive-value="false"
            active-text="启用"
            inactive-text="停用"
            :style="switchStyle"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="积分" prop="point">
          <el-input
            v-model.number="newFormInline.point"
            clearable
            placeholder="请输入积分"
          />
        </el-form-item>
      </re-col>
    </el-row>
  </el-form>
</template>
